Skip to content

gh-148688: Fix _BlocksOutputBuffer_Finish() double free#148689

Open
vstinner wants to merge 3 commits intopython:mainfrom
vstinner:writer_discard
Open

gh-148688: Fix _BlocksOutputBuffer_Finish() double free#148689
vstinner wants to merge 3 commits intopython:mainfrom
vstinner:writer_discard

Conversation

@vstinner
Copy link
Copy Markdown
Member

@vstinner vstinner commented Apr 17, 2026

If _BlocksOutputBuffer_Finish() fails (memory allocation failure), PyBytesWriter_Discard() is called on the writer. Then if _BlocksOutputBuffer_OnError() is called, it calls again PyBytesWriter_Discard() causing a double free.

Fix _BlocksOutputBuffer_Finish() by setting buffer->writer to NULL, so _BlocksOutputBuffer_OnError() does nothing instead of calling PyBytesWriter_Discard() again.

If _BlocksOutputBuffer_Finish() fails (memory allocation failure),
PyBytesWriter_Discard() is called on the writer. Then if
_BlocksOutputBuffer_OnError() is called, it calls again
PyBytesWriter_Discard() causing a double free.

Fix _BlocksOutputBuffer_Finish() by setting buffer->writer to NULL,
so _BlocksOutputBuffer_OnError() does nothing instead of calling
PyBytesWriter_Discard() again.
@emmatyping
Copy link
Copy Markdown
Member

You may want to also list compression.zstd, since it also uses BlocksOutputBuffer

@vstinner
Copy link
Copy Markdown
Member Author

You may want to also list compression.zstd, since it also uses BlocksOutputBuffer

Ah yes! I updated the NEWS entry.

Thanks for the review!

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@@ -0,0 +1,2 @@
:mod:`bz2`, :mod:`compression.zstd`, :mod:`lzma`, :mod:`zlib`: Fix a double
free in on memory allocation failure. Patch by Victor Stinner.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"in on"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants